<?php
/**
 * Created by PhpStorm.
 * User: LiuJiaJun
 * Date: 2019/1/13
 * Time: 15:40
 */

namespace api\storage\service\inventory;

use api\storage\common\Constant;
use api\storage\model\ReelsModel;
use api\storage\service\SocketService;
use api\storage\service\StorageService;
use service\file\CommonFile;
use think\Exception;

class WhiteMaterielInventoryService extends BaseMaterielInventoryService
{
    private $isWhite = true;

    protected function isWhite()
    {
        return $this->isWhite;
    }

    protected function afterHandleLightOff($seatId, $storageUuid)
    {
        SocketService::send($seatId, $storageUuid, Constant::TURN_OFF_LIGHT);
    }

    protected function getReels($code, $storageUuid)
    {
        $materielId = $code;
        $baseFile = new CommonFile($storageUuid, $this->basePath);
        $baseContent = $this->jsonDecode($baseFile->read());
        list($baseContent, $color) = $this->findColor($code, $baseContent);
        $baseFile->write($baseContent);

        $reelsModel = new ReelsModel();
        $result = $reelsModel->queryByMaterielIdWithSeat($materielId, $storageUuid)->toArray();
        $seatIds = array_column($result, 'seatId');
        SocketService::send($seatIds, $storageUuid, $color);
        return [
            'reels' => $result,
            'seatId' => $seatIds
        ];
    }

    /**
     * 此处用于清除base.json中物料的占用
     *
     * @param $code
     * @param $storageUuid
     * @param $isOccupy
     * @return bool|mixed
     */
    protected function remakeFlag($code, $storageUuid, $isOccupy)
    {
        $baseFile = new CommonFile($storageUuid, $this->basePath);
        $content = $this->jsonDecode($baseFile->read());
        if (!$isOccupy) {
            $index = array_search($code, array_column($content, 'materielId'));
            if ($index !== false) {
                $content[$index]['materielId'] = '';
            }
            $baseFile->write($content);
        }
        return true;
    }

    /**
     * 查找灯的颜色
     *
     * @param $code
     * @param $baseContent
     * @return array
     * @throws Exception
     */
    private function findColor($code, $baseContent)
    {
        if (empty($baseContent)) {
            $baseContent[] = [
                'materielId' => $code,
                'color' => 1
            ];
            return [$baseContent, 1];
        }
        //color 的值是数组下标 + 1
        $count = count($baseContent);
        if ($count < 3) {
            $baseContent[$count] = [
                'materielId' => $code,
                'color' => $count + 1
            ];
            $color = $count + 1;
        } else {
            $index = array_search('', array_column($baseContent, 'materielId'));
            if ($index !== false) {
                $baseContent[$index]['materielId'] = $code;
                $color = $baseContent[$index]['color'];
            } else {
                //如果没有找到的话，在尝试找一下有没有已经是该物料的信息
                $index = array_search($code, array_column($baseContent, 'materielId'));
                if ($index !== false) {
                    $color = $baseContent[$index]['color'];
                } else {
                    throw new Exception('根据物料盘点没有找到合适的灯的颜色');
                }
            }
        }
        return [$baseContent, $color];
    }
}